package com.fs.uc.nacos;

import com.alibaba.nacos.api.exception.NacosException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fs.uc.base.exception.CustomException;
import com.fs.uc.nacos.dto.Enterprise;
import com.fs.uc.nacos.dto.RemoteResponse;
import com.fs.uc.nacos.dto.Staff;
import com.fs.uc.nacos.dto.TenantStaff;
import org.wildfly.common.Assert;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

/**
 * @author 崔超
 * 2021/12/13
 */
@ApplicationScoped
public class BasicInfoService {
    private static final String SERVICE_NAME = "BasicInfo";
    @Inject
    NacosRegister nacosRegister;

    public Enterprise getEnterpriseById(Long enterpriseId) throws JsonProcessingException {
        String url = "/basic/inner/enterprise?id=" + enterpriseId;
        String result = get(url);
        try {
            RemoteResponse<Enterprise> response = new ObjectMapper().readValue(result, new TypeReference<>() {
            });
            if (!response.success) {
                throw new RuntimeException(response.msg);
            }
            return response.data;
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    public Staff getStaff(Long enterpriseId, Long userId) {
        String url = String.format("/basic/inner/staff/info?enterpriseId=%s&userId=%s", enterpriseId, userId);
        String result = get(url);
        try {
            RemoteResponse<Staff> response = new ObjectMapper().readValue(result, new TypeReference<>() {
            });
            if (!response.success) {
                throw new RuntimeException(response.msg);
            }
            return response.data;
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    public TenantStaff getTenantStaff(Long tenantId, Long userId) {
        String url = String.format("/basic/inner/tenant_staff/info?tenantId=%s&userId=%s", tenantId, userId);
        String result = get(url);
        try {
            RemoteResponse<TenantStaff> response = new ObjectMapper().readValue(result, new TypeReference<>() {
            });
            if (!response.success) {
                throw new RuntimeException(response.msg);
            }
            return response.data;
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    private String get(String path) {
        try {
            String baseUrl = nacosRegister.getInstance(SERVICE_NAME);
            String url = "http://" + baseUrl + path;
            String result = HttpUtil.get(url, null);
            if (result == null) {
                throw new CustomException("获取数据为空");
            }
            return result;
        } catch (NacosException e) {
            e.printStackTrace();
            throw new RuntimeException("获取基础服务nacos示例异常");
        }
    }
}
